package com.dhcc.DRGsHosp.application.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.CostSubApplication;
import com.dhcc.DRGsHosp.application.DictionaryDataApplication;
import com.dhcc.DRGsHosp.core.domain.CostSub;

@Named("drgsHospCostSubApplicationImpl20181212")
@Transactional
public class CostSubApplicationImpl implements CostSubApplication {
	@Autowired
	private DictionaryDataApplication dictionaryDataApplication ;
	
	@Autowired
	private JdbcTemplateObj jdbcTemplateObj;
	
	@Autowired
	private NamedParameterJdbcTemplate jdbcTempSeverice;
	
    private EntityRepository repository;
    
    private EntityRepository getRepository() {
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}

	public CostSub getCostSub(String costSubcode) {
		return CostSub.get(CostSub.class, costSubcode);
	}
	
	public void creatCostSub(CostSub costSub) {
		costSub.save();
	}
	
	public void updateCostSub(CostSub costSub) {
		costSub .save();
	}
	
	public void removeCostSub(CostSub costSub) {
		if(costSub != null){
			costSub.remove();
		}
	}
	
	public void removeCostSubs(Set<CostSub> costSubs) {
		for (CostSub costSub : costSubs) {
			costSub.remove();
		}
	}

	/**
	 * @return
	 * @throws
	 * @Description: 删除所有费用子项数据
	 * @author chenhanlin
	 * @date 2018/9/4 13:51
	 */
	@Override
	public void removeCostSubs() {
		String sql = "delete from drgs.T_COSTSUB" ;
		jdbcTemplateObj.update(sql) ;
	}

	public List<CostSub> findAllCostSub() {
		return CostSub.findAll(CostSub.class);
	}
	

	@Override
	public int saveCostSubInfo(String costSubInfo) {
		StringBuilder procSql = new StringBuilder();
		try {
			procSql.append("begin \n");
			String[] costSubs = costSubInfo.split("@");
			int csCnt = costSubs.length;
			for(int i=0;i<csCnt;i++)
			{
				String[] csDetails = costSubs[i].split("#");
				if(csDetails.length==3)
				{
					procSql.append("update drgs.t_costsub cs set cs.COST_SUB_ALIAS = '"+csDetails[1]+"',cs.PARCOST_CATE_CODE = '"+csDetails[2]+"' where cs.COST_SUB_CODE= '"+csDetails[0]+"'; \n");
				}
				else if(csDetails.length==2)
				{
					procSql.append("update drgs.t_costsub cs set cs.COST_SUB_ALIAS = '"+csDetails[1]+"',cs.PARCOST_CATE_CODE = '' where cs.COST_SUB_CODE= '"+csDetails[0]+"'; \n");
				}
				else 
				{
					procSql.append("update drgs.t_costsub cs set cs.COST_SUB_ALIAS = '',cs.PARCOST_CATE_CODE = '' where cs.COST_SUB_CODE= '"+csDetails[0]+"'; \n");
				}
			}
			procSql.append("commit; \n");
			procSql.append("end; \n");
			int udCnt = getRepository().createSqlQuery(procSql.toString()).executeUpdate();
			return udCnt;
		} catch (Exception e) {
			return -1;
		}
	}

	@Override
	public List<Map<String,Object>> getAllCostSub() {
		String sql = "select tcs.cost_sub_code,tcs.cost_sub_desc,nvl(tcs.cost_sub_alias,tcs.cost_sub_desc) cs_alias,tcs.dcc_code,tcs.parcost_cate_code from drgs.t_costsub tcs";
		List<Map<String,Object>> values = jdbcTemplateObj.queryForList(sql);
		return values;
	}
/**
 *  @Description：
 *  @Author lhj 
 *  @see com.dhcc.DRGsHosp.application.CostSubApplication#getExpenseType()
 */
	@Override
	public List<Map<String,Object>> getExpenseType() {
		List<Map<String,Object>>  ddList = dictionaryDataApplication.findDictionaryDataByCode("EXPENSE_TYPE");
		return ddList;
	}

	@Override
	public int queryForInt(String sql, Map<String, Object> condition) {
		return jdbcTempSeverice.queryForObject(sql, condition, Integer.class) ;
	}

}
